<?php
namespace SixShop\News\Service;

// The original Model is no longer used directly by this service.
// use SixShop\News\Model\News;

class NewsService
{
    private NewsFfiService $ffiService;

    public function __construct()
    {
        // Get the singleton instance of our FFI wrapper
        $this->ffiService = NewsFfiService::getInstance();
    }

    /**
     * 获取文章列表
     */
    public function getList($where = [], $order = ['is_top' => 'desc', 'id' => 'desc'], $limit = 20, $page = 1)
    {
        // 强制将分页参数转为整型，避免通过 query 传入的字符串在 JSON 编码后导致 Go 端反序列化失败
        $limit = (int) $limit;
        $page  = (int) $page;

        $params = [
            'where' => empty($where) ? new \stdClass() : $where,
            'order' => is_array($order) ? implode(', ', array_map(fn($k, $v) => "$k $v", array_keys($order), $order)) : $order,
            'limit' => $limit,
            'page'  => $page,
        ];
        return $this->ffiService->getNewsList($params);
    }

    /**
     * 获取单个文章详情
     */
    public function getById($id)
    {
        return $this->ffiService->getNewsById($id);
    }

    /**
     * 新增文章
     */
    public function create($data)
    {
        return $this->ffiService->createNews($data);
    }

    /**
     * 编辑文章
     */
    public function update($id, $data)
    {
        return $this->ffiService->updateNews($id, $data);
    }

    /**
     * 删除文章（软删除）
     */
    public function delete($id)
    {
        return $this->ffiService->deleteNews($id);
    }

    /**
     * 增加文章阅读数
     */
    public function incrementViews($id)
    {
        return $this->ffiService->incrementNewsViews($id);
    }
} 